package com.zx.mobile.service.proxy;

import com.zx.jd.util.JsonUtils;
import com.zx.map.service.model.AgentInfoModel;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.map.service.model.OrdLogModel;
import com.zx.map.utils.SUConstants;
import com.zx.mobile.MobileCommonHelper;
import com.zx.mobile.domain.MobileUserDomain;
import com.zx.platform.utils.StrUtils;
import com.zx.scp.utils.PropertyUtil;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.Map;

public class IncomePayProxy {
    private static Logger log = Logger.getLogger(IncomePayProxy.class);
    /**
     * 模态转换
     *
     * @param user
     * @return
     */
    public static OrdLogModel transformToOrdLog(MobileUserDomain user,String payType,String transType) {
        // 保存订单信息
        OrdLogModel ordLogModel = new OrdLogModel();
        ordLogModel.setAgentId(user.getMerchantInfoDetailModel().getAgentId());
        ordLogModel.setAgentSysAccount(user.getMerchantInfoDetailModel()
                .getAgentSysAccount());
        ordLogModel.setCreateDate(new Date());
        ordLogModel.setMerchantId(user.getMerchantInfoDetailModel()
                .getMerchantId());
        ordLogModel.setMerSysAccount(user.getMerchantInfoDetailModel()
                .getSysAccount());

        ordLogModel.setTransType(transType);//收款类型
        ordLogModel.setPaymentType(payType);//weixin  zhifubao
        ordLogModel.setTransStatus(MobileCommonHelper.INCOMESTATUS_NORMAL);//设置订单初始状态
        return ordLogModel;
    }

    /**
     * 模态转换
     *
     * @param model
     * @return
     */
    public static OrdLogModel transformMerchantToOrdLog(MerchantInfoModel model, AgentInfoModel agentInfoModel,String payType) {
        // 保存订单信息
        OrdLogModel ordLogModel = new OrdLogModel();
        ordLogModel.setAgentId(model.getAgentId());
        ordLogModel.setAgentSysAccount(agentInfoModel.getSysAccount());
        ordLogModel.setCreateDate(new Date());
        ordLogModel.setMerchantId(model.getMerchantId());
        ordLogModel.setMerSysAccount(model.getSysAccount());
        ordLogModel.setTransType(SUConstants.TRANSTYPE_OFFLINE_INCOME);//线下扫码收款
        ordLogModel.setPaymentType(payType);//weixin  zhifubao
        ordLogModel.setTransStatus(MobileCommonHelper.INCOMESTATUS_NORMAL);//设置订单初始状态
        return ordLogModel;
    }


    /**
     * 线上生成二维码收款
     * @param incomeAmt
     * @param orderId
     * @param payType
     * @return
     */
    public static Map getOnlineIncomePay(String incomeAmt, String orderId, String payType,String openId) {
        String baseUrl;
        if (SUConstants.paymentTypeWX.equals(payType)) {
            baseUrl= PropertyUtil.getValue("pay_online_wx_address");
        } else {
            baseUrl= PropertyUtil.getValue("pay_online_alipay_address");
        }
       return getResultMap(baseUrl,incomeAmt,orderId,payType,openId);
    }


    /**
     * 线下二维码收款
     * @param incomeAmt
     * @param orderId
     * @param payType
     * @return
     */
    public static Map getOfflineIncomePay(String incomeAmt, String orderId, String payType,String openId) {
        String baseUrl;
        if (SUConstants.paymentTypeWX.equals(payType)) {
            baseUrl= PropertyUtil.getValue("pay_offline_wx_address");
        } else {
            baseUrl= PropertyUtil.getValue("pay_offline_alipay_address");
        }
        return getResultMap(baseUrl,incomeAmt,orderId,payType,openId);
    }

    /**
     * 线上充值
     * @param incomeAmt
     * @param orderId
     * @param payType
     * @return
     */
    public static Map getRechargePay(String incomeAmt, String orderId, String payType,String openId) {
        String baseUrl;
        if (SUConstants.paymentTypeWX.equals(payType)) {
            baseUrl= PropertyUtil.getValue("pay_recharge_wx_address");
        } else {
            baseUrl= PropertyUtil.getValue("pay_recharge_alipay_address");
        }
        return getResultMap(baseUrl,incomeAmt,orderId,payType,openId);
    }

    private static Map getResultMap(String baseUrl, String incomeAmt, String orderId, String payType, String openId) {
        String requestUrl = baseUrl + "?amount=" + incomeAmt + "&orderId=" + orderId + "&payType="+payType;
        if(StrUtils.isNotBlank(openId)){
            requestUrl+="&openId="+openId;
        }
        Map map=MobileCommonHelper.httpRequest(Map.class, requestUrl);
        log.info("二维码接口返回信息:" + JsonUtils.beanToJson(map));
        return map;
    }
}
